/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 *
 */

import process from 'process';

import { OutputType } from '../../common/daemon-protocol/common-enum.js';
import { defaultSessionManager, SESSION_EMIT_LOG } from '../session/session-manager.js';

/**
 * 覆写输出流与错误流的write方法
 */
export function overridePrintWrite() {
  process.stdout.write = function (chunk: string | Uint8Array, encoding?: any) {
    defaultSessionManager.emit(SESSION_EMIT_LOG, OutputType.STDOUT, chunk, encoding);
    return true;
  };

  process.stderr.write = function (chunk: string | Uint8Array, encoding?: any) {
    defaultSessionManager.emit(SESSION_EMIT_LOG, OutputType.STDERR, chunk, encoding);
    return true;
  };
}
